<template>
  <a-modal :title="title" :width="1200" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen
    @ok="handleOk" @cancel="handleCancel" cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标题">
          <a-input placeholder="请输入标题" v-decorator="['title', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="作者">
          <a-input placeholder="请输入作者" v-decorator="['author', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="缩略图">
          <div>
          	<img id='example_video' class="image" style="width: 200px;" v-if="coverImg"
          		:src="decodeURIComponent(coverImg)" />
          </div>
          <a-upload :multiple="true" :file-list="coverImgList" :remove="handleRemove"
          	:before-upload="beforeUpload">
          	<a-button>
          		<a-icon type="upload" /> 上传缩略图
          	</a-button>
          </a-upload>
          <a-button type="primary" :disabled="coverImgList.length === 0" :loading="uploading"
          	style="margin-top: 16px" @click="handleUpload">
          	{{ uploading ? '上传中' : '开始上传' }}
          </a-button>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="内容详情">
          <j-editor style="width: 100%" v-decorator="[ 'content', {} ]" triggerChange></j-editor>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="点击量">
          <a-input-number v-decorator="[ 'hits', {}]" />
        </a-form-item>
        <!-- <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-input placeholder="请输入状态:0=隐藏,1=显示" v-decorator="['status', {}]" />
        </a-form-item> -->
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="附件">
          <!-- <a-input placeholder="请输入附件，数组字符串" v-decorator="['attachments', {}]" /> -->
          <div>
          	<div v-if="attachments" style="color: blue;">{{attachments}}</div>
          </div>
          <a-upload :multiple="true" :file-list="attachmentsList" :remove="handleRemove1"
          	:before-upload="beforeUpload1">
          	<a-button>
          		<a-icon type="upload" /> 上传附件
          	</a-button>
          </a-upload>
          <a-button type="primary" :disabled="attachmentsList.length === 0" :loading="uploading1"
          	style="margin-top: 16px" @click="handleUpload1">
          	{{ uploading1 ? '上传中' : '开始上传' }}
          </a-button>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
  	httpAction,
  	postAction
  } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JEditor from '@/components/jeecg/JEditor'

  export default {
    components: {
      JEditor
    },
    name: "YsyHotnewsModal",
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          hotSpotId: {
            rules: [{
              required: true,
              message: '请输入热点聚焦id!'
            }]
          },
        },
        url: {
          add: "/app/ysyHotnews/add",
          edit: "/app/ysyHotnews/edit",
          upload: '/app/utils/aliOssUpload',
        },
        coverImg: '',
        uploading: false,
        coverImgList: [],
        attachments: '',
        uploading1: false,
        attachmentsList: [],
      }
    },
    created() {},
    methods: {
      handleUpload() {
      	const {
      		coverImgList
      	} = this;
      	const formData = new FormData();
      	coverImgList.forEach(file => {
      		formData.append('file', file);
      	});
      	this.uploading = true;
      
      	postAction(this.url.upload, formData).then(res => {
      		this.coverImgList = [];
      		this.uploading = false;
      		this.coverImg = res.message.toString()
      	}).catch(e => {
      		this.uploading = false;
      		this.$message.error('上传失败');
      	})
      },
      handleRemove(file) {
      	const index = this.coverImgList.indexOf(file);
      	const newFileList = this.coverImgList.slice();
      	newFileList.splice(index, 1);
      	this.coverImgList = newFileList;
      },
      beforeUpload(file) {
      	this.coverImgList = [...this.coverImgList, file];
      	return false;
      },
      
      
      handleUpload1() {
      	const {
      		attachmentsList
      	} = this;
      	const formData = new FormData();
      	attachmentsList.forEach(file => {
      		formData.append('file', file);
      	});
      	this.uploading1 = true;
      
      	postAction(this.url.upload, formData).then(res => {
      		this.attachmentsList = [];
      		this.uploading1 = false;
      		this.attachments = res.message.toString()
      	}).catch(e => {
      		this.uploading1 = false;
      		this.$message.error('上传失败');
      	})
      },
      handleRemove1(file) {
      	const index = this.attachmentsList.indexOf(file);
      	const newFileList = this.attachmentsList.slice();
      	newFileList.splice(index, 1);
      	this.attachmentsList = newFileList;
      },
      beforeUpload1(file) {
      	this.attachmentsList = [...this.attachmentsList, file];
      	return false;
      },
      
      add() {
        this.edit({});
      },
      edit(record) {
        record.remark = "油速运热点聚焦的内容均来自于网络，油速运不对其完整性、真实性、合法性等负责；其内容并不代表油速运的观点。如果相关内容侵害您的相关著作等权利，请联系客服，我们会尽快处理相应内容。"
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.coverImg = record.image
        this.attachments = record.attachments
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'hotSpotId', 'title', 'author', 'image', 'content', 'hits',
            'status', 'attachments', 'remark'))
          //时间格式化
        });

      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化

            console.log(formData)
            formData.image = this.coverImg
            formData.attachments = this.attachments
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel() {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>